package com.miao.websecurity.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.miao.websecurity.entity.RoleAdmin;
import com.miao.websecurity.entity.RoleMenu;
import com.miao.websecurity.entity.vo.MenuVO;
import com.miao.common.entity.R;
import com.miao.common.utils.JWTUtil;
import com.miao.websecurity.entity.Admin;
import com.miao.websecurity.entity.SysMenu;
import com.miao.websecurity.service.AdminService;
import com.miao.websecurity.service.RoleAdminService;
import com.miao.websecurity.service.RoleMenuService;
import com.miao.websecurity.service.SysMenuService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author MiaoWenqing
 * @description: TODO
 * @date 2021/2/1 17:45
 */
@RestController
@RequestMapping("/admin/menu")
public class MenuController {


    @Resource
    private SysMenuService sysMenuService;

    @Resource
    private AdminService adminService;
    @Resource
    private RoleAdminService roleAdminService;
    @Resource
    private RoleMenuService roleMenuService;


    @PostMapping("/add")
    public R menuAdd(@RequestBody SysMenu sysMenu, @RequestHeader(value = "access-token") String token) {

        String s = JWTUtil.analysisToken(token);
        Map map = JSON.parseObject(s, Map.class);
        Admin admin = this.adminService.getById(map.get("adminId").toString());
        if (admin.getAdminType() == 1 && null == admin.getStoreId()) {
            sysMenu.setMenuType(true);
        } else {
            sysMenu.setMenuType(false);
        }
        if (this.sysMenuService.save(sysMenu)) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @GetMapping("/all")
    public R menuList(@RequestHeader(value = "access-token") String token) {
        String s = JWTUtil.analysisToken(token);
        Map map = JSON.parseObject(s, Map.class);
        Admin admin = this.adminService.getById(map.get("adminId").toString());
        List<SysMenu> list = this.sysMenuService.list();
        ArrayList<MenuVO> sysMenus = new ArrayList<>();
        if (admin.getAdminType() == 1 && null == admin.getStoreId()) {
            //平台管理员
            list.forEach(item -> {
                //平台管理员 一级菜单
                if ((null == item.getMenuType() || item.getMenuType()) && null == item.getMenuPar()) {
                    MenuVO menuVO = new MenuVO();
                    menuVO.setChildren(new ArrayList<SysMenu>());
                    menuVO.setMenuDes(item.getMenuDes());
                    menuVO.setMenuIcon(item.getMenuIcon());
                    menuVO.setMenuId(item.getMenuId());
                    menuVO.setMenuType(item.getMenuType());
                    menuVO.setMenuName(item.getMenuName());
                    menuVO.setMenuRouter(item.getMenuRouter());

                    list.forEach(item1 -> {
                        if ((null == item1.getMenuType() || item1.getMenuType()) && menuVO.getMenuId().equals(item1.getMenuPar())) {
                            menuVO.getChildren().add(item1);
                        }

                    });
                    sysMenus.add(menuVO);
                }


            });

        } else {
            //店铺管理员
            list.forEach(item -> {
                //平台管理员 一级菜单
                if ((null == item.getMenuType() || !item.getMenuType()) && null == item.getMenuPar()) {
                    MenuVO menuVO = new MenuVO();
                    menuVO.setChildren(new ArrayList<SysMenu>());
                    menuVO.setMenuDes(item.getMenuDes());
                    menuVO.setMenuIcon(item.getMenuIcon());
                    menuVO.setMenuId(item.getMenuId());
                    menuVO.setMenuType(item.getMenuType());
                    menuVO.setMenuName(item.getMenuName());
                    menuVO.setMenuRouter(item.getMenuRouter());
                    list.forEach(item1 -> {
                        if ((null == item1.getMenuType() || !item1.getMenuType()) && menuVO.getMenuId().equals(item1.getMenuPar())) {
                            menuVO.getChildren().add(item1);
                        }

                    });
                    sysMenus.add(menuVO);
                }
            });

        }
        return R.ok(sysMenus);
    }


    @GetMapping("/allById")
    public R menuListByAdminId(@RequestHeader(value = "access-token") String token) {
        String s = JWTUtil.analysisToken(token);
        Map map = JSON.parseObject(s, Map.class);
        Admin admin = this.adminService.getById(map.get("adminId").toString());

        List<SysMenu> list = new ArrayList<>();
        QueryWrapper<RoleAdmin> roleAdminQueryWrapper = new QueryWrapper<>();
        roleAdminQueryWrapper.eq("admin_id", admin.getAdminId());
        List<RoleAdmin> list1 = this.roleAdminService.list(roleAdminQueryWrapper);
        for (RoleAdmin roleAdmin : list1) {
            QueryWrapper<RoleMenu> roleMenuQueryWrapper = new QueryWrapper<>();
            roleMenuQueryWrapper.eq("role_id", roleAdmin.getRoleId());
            List<RoleMenu> list2 = this.roleMenuService.list(roleMenuQueryWrapper);
            for (RoleMenu roleMenu : list2) {
                if (roleMenu.getMenuId().equals("-1")) {

                    list = this.sysMenuService.list();
                    break;
                }

            }
            if (list.size() > 0) break;

        }
        if (list.size() <= 0) {
            list = this.sysMenuService.getMenuByAdminId(map.get("adminId").toString());
        }


        ArrayList<MenuVO> sysMenus = new ArrayList<>();
        if (admin.getAdminType() == 1 && null == admin.getStoreId()) {
            //平台管理员
            List<SysMenu> finalList = list;
            list.forEach(item -> {
                //平台管理员 一级菜单
                if ((null == item.getMenuType() || item.getMenuType()) && null == item.getMenuPar()) {
                    MenuVO menuVO = new MenuVO();
                    menuVO.setChildren(new ArrayList<SysMenu>());
                    menuVO.setMenuDes(item.getMenuDes());
                    menuVO.setMenuIcon(item.getMenuIcon());
                    menuVO.setMenuId(item.getMenuId());
                    menuVO.setMenuType(item.getMenuType());
                    menuVO.setMenuName(item.getMenuName());
                    menuVO.setMenuRouter(item.getMenuRouter());

                    finalList.forEach(item1 -> {
                        if ((null == item1.getMenuType() || item1.getMenuType()) && menuVO.getMenuId().equals(item1.getMenuPar())) {
                            menuVO.getChildren().add(item1);
                        }

                    });
                    sysMenus.add(menuVO);
                }


            });

        } else {
            //店铺管理员
            List<SysMenu> finalList1 = list;
            list.forEach(item -> {
                //平台管理员 一级菜单
                if ((null == item.getMenuType() || !item.getMenuType()) && null == item.getMenuPar()) {
                    MenuVO menuVO = new MenuVO();
                    menuVO.setChildren(new ArrayList<SysMenu>());
                    menuVO.setMenuDes(item.getMenuDes());
                    menuVO.setMenuIcon(item.getMenuIcon());
                    menuVO.setMenuId(item.getMenuId());
                    menuVO.setMenuType(item.getMenuType());
                    menuVO.setMenuName(item.getMenuName());
                    menuVO.setMenuRouter(item.getMenuRouter());
                    finalList1.forEach(item1 -> {
                        if ((null == item1.getMenuType() || !item1.getMenuType()) && menuVO.getMenuId().equals(item1.getMenuPar())) {
                            menuVO.getChildren().add(item1);
                        }

                    });
                    sysMenus.add(menuVO);
                }
            });

        }
        return R.ok(sysMenus);
    }

    @GetMapping("allFather")
    public R menuFather(@RequestHeader(value = "access-token") String token) {
        String s = JWTUtil.analysisToken(token);
        Map map = JSON.parseObject(s, Map.class);
        Admin admin = this.adminService.getById(map.get("adminId").toString());
        List<SysMenu> list = this.sysMenuService.list();
        ArrayList<MenuVO> sysMenus = new ArrayList<>();
        if (admin.getAdminType() == 1 && null == admin.getStoreId()) {
            //平台管理员
            list.forEach(item -> {
                //平台管理员 一级菜单
                if ((null == item.getMenuType() || item.getMenuType()) && null == item.getMenuPar()) {
                    MenuVO menuVO = new MenuVO();
                    menuVO.setChildren(null);
                    menuVO.setMenuDes(item.getMenuDes());
                    menuVO.setMenuIcon(item.getMenuIcon());
                    menuVO.setMenuId(item.getMenuId());
                    menuVO.setMenuType(item.getMenuType());
                    menuVO.setMenuName(item.getMenuName());
                    sysMenus.add(menuVO);
                }


            });

        } else {
            //店铺管理员
            list.forEach(item -> {
                //店铺管理员 一级菜单
                if ((null == item.getMenuType() || !item.getMenuType()) && null == item.getMenuPar()) {
                    MenuVO menuVO = new MenuVO();
                    menuVO.setChildren(null);
                    menuVO.setMenuDes(item.getMenuDes());
                    menuVO.setMenuIcon(item.getMenuIcon());
                    menuVO.setMenuId(item.getMenuId());
                    menuVO.setMenuType(item.getMenuType());
                    menuVO.setMenuName(item.getMenuName());
                    sysMenus.add(menuVO);
                }


            });

        }


        return R.ok(sysMenus);
    }


    @GetMapping("/del")
    public R menuDel(String menuId) {
        if (this.sysMenuService.removeById(menuId)) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @PostMapping("/edit")
    public R menuEdit(@RequestBody SysMenu sysMenu) {

        if (this.sysMenuService.saveOrUpdate(sysMenu)) {
            return R.ok();
        } else {
            return R.error();
        }

    }


}
